<?php
namespace app\api\controller\v2\controller\pushMessage;

use app\api\controller\v2\model\pet\Appointment as Ap;
use think\Controller;
use app\api\controller\Api;
use think\Model;
use think\Db;
use think\Image;
use think\Request;
use JPush\Client as JPush;
use think\Response;
use app\api\controller\UnauthorizedException;
use think\Validate;


class PushMessage extends api
{



    // 发送自定义消息
    public function custom_message($user_id,$message){
        $appkey = 'a3d1bad973276db7cee3f40a';
        $master_secret = 'ac5c6c6b1101643082c88913';
        $client = new JPush($appkey,$master_secret);
        $r_id = Db::query("select registration_id from ym_device_no where user_id = '$user_id'");
        $registration_id = $r_id[0]['registration_id'];
        $payload = $client->push()
            ->setPlatform('all')
            ->addAlias($user_id)
            //设备号
            ->addRegistrationId($registration_id)
            ->message($message, [
                'title' => 'Tix',
                'content_type' => 'text',
                'extras' => [
                    'logout' => '1'
                ]
            ])
            ->options(array(
                "apns_production" => true  //true表示发送到生产环境(默认值)，false为开发环境
            ));
        $payload->send(); // 执行推送
    }


    //显示消息（关注 判断是否互相关注 点赞 评论 回复 打赏  预约 预约回复  寄养 寄养回复）
    public function show_message($user_id,$pageno){

        $pagesize = 10;
        $startno = $pageno *$pagesize;
        $arr = [];
        $result = Db::query("select user_id,m_id from ym_message 
                            where be_user_id = '$user_id' 
                            order by create_time 
                            desc limit {$startno},{$pagesize}");
        $length = count($result);
        for ($i=0; $i < $length; $i++) {
            $m_id = $result[$i]['m_id'];
            // 关注
            $follow = Db::query("select c.user_name,c.user_icon,b.m_id,b.user_id,b.message_type,b.create_time 
                                    from ym_message_follow as a 
                                    join ym_message as b on a.m_id = b.m_id 
                                    join user_info as c on a.user_id = c.user_id 
                                    where a.m_id = '$m_id'");
            if (!empty($follow)) {
                //判断是否互相关注
                $follow_id = $result[$i]['user_id'];
                $follow_or_not = Db::table('ym_user_follow')->where('user_id',$user_id)->where('follow_id',$follow_id)->select();
                if (!empty($follow_or_not)) {
                    $follow[0]['follow_status'] = 1;//你也关注了对方
                }else{
                    $follow[0]['follow_status'] = 0;//你还没关注对方
                }
                $arr [] = $follow[0];
            }

            // 点赞
            $upvote = Db::query("select c.user_name,c.user_icon,a.module_type,a.id as ID,a.cmmt_id,a.img_url,b.m_id,b.user_id,b.message_type,b.create_time 
                                from ym_message_upvote as a 
                                join ym_message as b on a.m_id = b.m_id 
                                join user_info as c on b.user_id = c.user_id 
                                where a.m_id = '$m_id'");
            if (!empty($upvote)) {
                $arr [] = $upvote[0];
            }
            // 评论
            $cmmt =  Db::query("select c.user_name,c.user_icon,a.module_type,a.id as ID,a.cmmt_id,a.content,a.img_url,b.m_id,b.user_id,b.message_type,b.create_time 
                                from ym_message_cmmt as a 
                                join ym_message as b on a.m_id = b.m_id 
                                join user_info as c on b.user_id = c.user_id 
                                where a.m_id = '$m_id'");
            if (!empty($cmmt)) {
                $arr [] = $cmmt[0];
            }
            //回复
            $reply =  Db::query("select c.user_name,c.user_icon,a.module_type,a.id as ID,a.cmmt_id,a.cmmt_content,a.img_url,b.m_id,b.user_id,b.message_type,b.create_time 
                                from ym_message_reply as a 
                                join ym_message as b on a.m_id = b.m_id 
                                join user_info as c on b.user_id = c.user_id 
                                where a.m_id = '$m_id'");
            if (!empty($reply)) {
                $arr [] = $reply[0];
            }
            // 打赏
            $reward =  Db::query("select c.user_name,c.user_icon,a.module_type,a.id as ID,a.food_count,a.img_url,b.m_id,b.user_id,b.message_type,b.create_time from ym_message_reward as a join ym_message as b on a.m_id = b.m_id join user_info as c on b.user_id = c.user_id where a.m_id = '$m_id'");
            if (!empty($reward)) {
                $arr [] = $reward[0];
            }
            // 预约
            $appointment =  Db::query("select c.user_name,c.user_icon,a.appointment_id,b.m_id,b.user_id,b.message_type,b.create_time 
                from ym_message_appointment as a 
                join ym_message as b on a.m_id = b.m_id 
                join user_info as c on b.user_id = c.user_id 
                where a.m_id = '$m_id'");
            if (!empty($appointment)) {
                $arr [] = $appointment[0];
            }
            // 预约回复
            $appointment_agree = Db::query("select c.user_name,c.user_icon,d.mobilephone,a.appointment_id,a.refuse_reason,b.m_id,b.user_id,b.message_type,b.create_time 
                from ym_message_appointment_agree_or_not as a 
                join ym_message as b on a.m_id = b.m_id 
                join user_info as c on b.user_id = c.user_id 
                join ym_user as d on b.user_id = d.id 
                where a.m_id = '$m_id'");
            if (!empty($appointment_agree)) {
                if (empty($appointment_agree[0]['refuse_reason'])) {
                    $appointment_agree[0]['is_agree'] = 1;
                }else{
                    $appointment_agree[0]['is_agree'] = 0;
                }
                $arr [] = $appointment_agree[0];
            }
            // 寄养
            $fosterage =  Db::query("select c.user_name,c.user_icon,a.fosterage_id,b.m_id,b.user_id,b.message_type,b.create_time 
                from ym_message_fosterage as a 
                join ym_message as b on a.m_id = b.m_id 
                join user_info as c on b.user_id = c.user_id 
                where a.m_id = '$m_id'");
            if (!empty($fosterage)) {
                $arr [] = $fosterage[0];
            }
            // 寄养回复
            $fosterage_agree = Db::query("select c.user_name,c.user_icon,d.mobilephone,a.fosterage_id,a.refuse_reason,b.m_id,b.user_id,b.message_type,b.create_time from ym_message_fosterage_agree_or_not as a join ym_message as b on a.m_id = b.m_id join user_info as c on b.user_id = c.user_id join ym_user as d on b.user_id = d.id where a.m_id = '$m_id'");
            if (!empty($fosterage_agree)) {
                if (empty($fosterage_agree[0]['refuse_reason'])) {
                    $fosterage_agree[0]['is_agree'] = 1;
                }else{
                    $fosterage_agree[0]['is_agree'] = 0;
                }
                $arr [] = $fosterage_agree[0];
            }

        }
        return $this->returnmsg(200,'message',$arr);
    }




    //推送全部
    public function push($user_id,$message){
        $appkey = 'a3d1bad973276db7cee3f40a';
        $master_secret = 'ac5c6c6b1101643082c88913';
        $client = new JPush($appkey,$master_secret);
        $r_id = Db::query("select registration_id from ym_device_no where user_id = '$user_id'");
        $registration_id = $r_id[0]['registration_id'];

        $push = $client->push();
        $push->setPlatform('all');
        //别名
        $push->addAlias($user_id);
        //设备号
        $push->addRegistrationId($registration_id);
        // $push->iosNotification();
        $push->iosNotification($message, [
            'sound' => 'sound.cat',
            'badge' => '+1',

        ]);
        $push->options(array(
            "apns_production" => true
        ));
        // try {
        $push->send();

    }

}